home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-01-19 | 10.2 KB | 400 lines | [TEXT/MPS ] |
- //_______________________________________________________________________________________________________________
- //___________________________________________________ INCLUDES __________________________________________________
- //_______________________________________________________________________________________________________________
-
- #include <Values.h>
- #include <Types.h>
- #include <CType.h>
- #include <Resources.h>
- #include <Menus.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <TextEdit.h>
- #include <Windows.h>
- #include <Memory.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <ToolUtils.h>
- #include <OSEvents.h>
- #include <Balloons.h>
- #include <Errors.h>
- #include <StandardFile.h>
- #include <String.h>
- #include <Strings.h>
- #include <Shutdown.h>
- #include <GestaltEqu.h>
- #include <stdio.h>
- #include <StdLib.h>
- #include <ErrMgr.h>
- #include <Errors.h>
- #include <Files.h>
- #include <Packages.h>
-
- #include "ASUpdateArticleUtilities.h"
- #include "ASUpdateFileUtilities.h"
- #include "UpdateMaker.h"
-
- //_______________________________________________________________________________________________________________
- //_________________________________________________ PROTOTYPING _________________________________________________
- //_______________________________________________________________________________________________________________
-
- void Initialize( void );
- void InitializeMenus( void );
- int main();
- void DoEventLoop( void );
- void DoMouseDown( EventRecord* evt );
- void DoKeyDown( EventRecord* evt );
- void DoMenuCommand( long menuResult );
-
- OSErr DoNew( void );
- Boolean MakeNewspaperFile(short* theNPRefNum);
- Boolean AddArticleFromFile(short theNPRefNum);
-
- Handle GetDIHandle( short item, DialogPtr dialog = nil );
- void GetDIText( short item, Str255& str, DialogPtr dialog );
-
- extern "C" _DataInit( void );
-
- //_______________________________________________________________________________________________________________
- //______________________________________________ IMPLEMENTATION _________________________________________________
- //_______________________________________________________________________________________________________________
-
- #pragma segment Initialize
-
- void Initialize( void )
- {
- UnloadSeg( _DataInit ); // clear init segment out of heap
- for ( short counter = 1; counter < 6; counter++ ) // this should give enough handles
- MoreMasters();
- InitGraf( ( Ptr )&qd.thePort ); // required stuff
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- MaxApplZone();
-
- InitializeMenus(); // load MENU res and draw
- }
-
- //_______________________________________________________________________________________________________________
-
- void InitializeMenus( void )
- {
- MenuHandle menu;
-
- AddResMenu( menu = GetMenu( mApple ), 'DRVR' ); // get list of DAs into apple menu
- InsertMenu( menu, 0 );
- InsertMenu( GetMenu( mFile ), 0 );
- InsertMenu( GetMenu( mEdit ), 0 );
-
- DrawMenuBar();
- }
-
- //_______________________________________________________________________________________________________________
-
- #pragma segment main
-
- int main()
- {
- Initialize(); // do startup things
- UnloadSeg( Initialize ); // we won't need that code ever again
- DoEventLoop();
- return 0; // dedicated ANSI standard follower
- }
-
- //_______________________________________________________________________________________________________________
-
- void DoEventLoop( void )
- {
- EventRecord evt;
-
- for (;;) {
- if ( WaitNextEvent( everyEvent, &evt, kSleepValue, ( RgnHandle )nil ) ) {
- switch (evt.what) {
- case mouseDown:
- DoMouseDown( &evt );
- break;
- case keyDown:
- DoKeyDown( &evt );
- break;
- default: // we don't care about other events
- break;
- }
- }
- }
- }
-
- //_______________________________________________________________________________________________________________
-
- void DoMouseDown( EventRecord* evt )
- {
- WindowPtr window;
-
- short part = FindWindow( evt->where, &window );
- switch ( part ) { // figure out where it was clicked
- case inMenuBar:
- DoMenuCommand( MenuSelect( evt->where ) );
- break;
- case inDrag: // window's title bar
- case inGoAway: // window's close box
- case inContent: // inside window
- case inGrow:
- break;
- case inSysWindow: // it's a DA window
- SystemClick( evt, window );
- break;
- }
- }
-
- //_______________________________________________________________________________________________________________
-
- void DoKeyDown( EventRecord* event )
- {
- if ( event->modifiers & cmdKey ) // a command key
- DoMenuCommand( MenuKey( ( char )event->message & charCodeMask ) );
- }
-
- //_______________________________________________________________________________________________________________
-
- void DoMenuCommand( long menuResult )
- {
- Str255 daName;
-
- short menuItem = LoWord( menuResult );
- switch ( HiWord( menuResult ) ) {
- case mApple:
- switch ( menuItem ) {
- case iAbout:
- SysBeep(20);
- break;
- default:
- GetItem( GetMHandle( mApple ), menuItem, daName);
- short daRefNum = OpenDeskAcc( daName );
- break;
- }
- break;
-
- case mFile:
- switch ( menuItem ) {
- case iNew:
- DoNew();
- break;
- case iQuit:
- ExitToShell(); // change it to PostCommand( ID_QUIT )
- break;
- }
- break;
-
- default: // ignore Edit menu
- break;
- }
- HiliteMenu(0);
- }
-
- //_______________________________________________________________________________________________________________
-
- OSErr DoNew( void )
- {
- OSErr err;
- short theNPRefNum;
- Boolean done = false;
-
- if (!MakeNewspaperFile(&theNPRefNum))
- return noErr;
-
- while (!done) {
- done = !AddArticleFromFile(theNPRefNum);
- }
-
- err = ASUCloseUpdateFile(theNPRefNum);
- return noErr;
- }
-
- //_______________________________________________________________________________________________________________
-
- Boolean MakeNewspaperFile(short* theNPRefNum)
- {
- OSErr err;
- StandardFileReply reply;
-
- StandardPutFile( "\pNewspaper name:", "\pIguana Daily", &reply );
- if (!reply.sfGood)
- return false;
- if ((err = ASUCreateUpdateFile(&reply.sfFile, 'MRLW', kAppleSearchTextUpdate, kVersion1)) != noErr)
- return false;
- if ((err = ASUOpenUpdateFile(&reply.sfFile, theNPRefNum)) != noErr)
- return false;
-
- return true;
- }
-
- //_______________________________________________________________________________________________________________
-
- Boolean AddArticleFromFile(short theNPRefNum)
- {
- enum {maxArticleSize = 8096};
-
- OSErr err;
- StandardFileReply reply;
- SFTypeList types;
-
- ASUArticleHeader theArticleInfo;
- ASUDataSize count;
- ASUDCPtr dc;
- Ptr buffer;
- short theSrcRefnum;
- long srcEOF;
- long readSize;
-
- char title[40];
-
- static long theArticleNumber = 1;
-
- // Open a source file
- types[0] = 'TEXT';
- StandardGetFile(NULL, 1, types, &reply);
- if (!reply.sfGood)
- return false;
- if ((err = FSpOpenDF(&reply.sfFile, fsRdPerm, &theSrcRefnum)) != noErr)
- return false;
-
- // Get file size & allocate buffer
- if ((err = GetEOF(theSrcRefnum, &srcEOF)) != noErr) {
- FSClose(theSrcRefnum);
- return false;
- }
- buffer = NewPtr(srcEOF);
- if (buffer == NULL) {
- FSClose(theSrcRefnum);
- return false;
- }
-
- {
- DateTimeRec DateTime;
- CInfoPBPtr pb = (CInfoPBPtr)NewPtrClear( sizeof ( CInfoPBRec ) );
- pb->hFileInfo.ioNamePtr = &reply.sfFile.name[0];
- pb->hFileInfo.ioVRefNum = reply.sfFile.vRefNum;
- pb->hFileInfo.ioDirID = reply.sfFile.parID;
-
- if ( ( err = PBGetCatInfo( pb, false ) ) != noErr ) {
- debugstr( "PBGetCatInfo error" );
- return false;
- }
- else {
- Secs2Date( pb->hFileInfo.ioFlMdDat, &DateTime );
- BlockMove(&DateTime, &theArticleInfo.articleDateTime, sizeof(ASUArticleDate));
- }
-
- if ( pb ) {
- DisposePtr( (Ptr)pb );
- pb = nil;
- }
- }
-
- // Read file into buffer
- readSize = srcEOF;
- if ((err = FSRead(theSrcRefnum, &readSize, buffer)) != noErr) {
- debugstr("FSRead error");
- FSClose(theSrcRefnum);
- return false;
- }
- if ((err = FSClose(theSrcRefnum)) != noErr) {
- debugstr("FSRead error");
- return false;
- }
- if (readSize != srcEOF) {
- debugstr("readSize != srcEOF");
- return false;
- }
-
-
- // Build the article info
- theArticleInfo.articleDataSize = readSize;
- theArticleInfo.articleType = kTextArticleItem;
-
- theArticleInfo.userBytes = theArticleNumber;
-
-
- BlockMove( (Ptr)&reply.sfFile.name[0], (Ptr)title, reply.sfFile.name[0] + 1 );
- p2cstr( (Str255)title );
-
- theArticleInfo.articleTitle = title;
-
- // prompt user for the source name
- {
- DialogPtr dialog = GetNewDialog( rSourceTextEditDialogID, nil, (WindowPtr) -1 );
- if ( dialog ) {
- short item = 0;
- WindowPtr saved = nil;
- Str255 source;
-
- GetPort( &saved );
- SetPort( dialog );
- ShowWindow( dialog );
- SelectWindow( dialog );
-
- do {
- ModalDialog( nil, &item ); // Call DM routine without filter proc
- } while ( item != diOKItem ); // Until OK button is clicked
-
- GetDIText( diSourceTextEditItem, source, dialog );
- if ( source[0] ) {
- theArticleInfo.articleSource = NewPtrClear( source[0] + 1 ); // +1 for null terminator
- BlockMove( (Ptr)&source[1], theArticleInfo.articleSource, source[0] );
- }
- else {
- theArticleInfo.articleSource = nil;
- }
-
- SetPort( saved ); // Change back the grafPort
- HideWindow( dialog );
- DisposeDialog( dialog );
- }
- else {
- return false;
- }
- }
-
- count = theArticleInfo.articleDataSize;
-
- if ((err = ASUAddArticle(theNPRefNum, &theArticleInfo, count, buffer, &dc)) != noErr)
- return false;
-
- if ( buffer ) {
- DisposePtr( buffer );
- buffer = nil;
- }
-
- return true;
- }
-
- //_______________________________________________________________________________________________________________
-
- Handle GetDIHandle( short item, DialogPtr dialog )
- {
- short type;
- Handle handle;
- Rect box;
-
- if ( dialog == nil )
- dialog = (DialogPtr)FrontWindow();
- GetDItem( dialog, item, &type, &handle, &box );
- return handle;
- }
-
- //_______________________________________________________________________________________________________________
-
- void GetDIText( short item, Str255& str, DialogPtr dialog )
- {
- Handle handle = GetDIHandle( item, dialog );
- GetIText( handle, str );
- }
-
- //_______________________________________________________________________________________________________________
-
-
-
-
-